package cn.iocoder.yudao.module.crm.controller.admin.cluetranslog;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.crm.controller.admin.cluetranslog.vo.*;
import cn.iocoder.yudao.module.crm.dal.dataobject.cluetranslog.ClueTransLogDO;
import cn.iocoder.yudao.module.crm.service.cluetranslog.ClueTransLogService;

@Tag(name = "管理后台 - 规则执行记录")
@RestController
@RequestMapping("/crm/clue-trans-log")
@Validated
public class ClueTransLogController {

    @Resource
    private ClueTransLogService clueTransLogService;


    @GetMapping("/page")
    @Operation(summary = "获得规则执行记录分页")
    public CommonResult<PageResult<ClueTransLogRespVO>> getClueTransLogPage(@Valid ClueTransLogPageReqVO pageReqVO) {
        PageResult<ClueTransLogDO> pageResult = clueTransLogService.getClueTransLogPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ClueTransLogRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出规则执行记录 Excel")
    @OperateLog(type = EXPORT)
    public void exportClueTransLogExcel(@Valid ClueTransLogPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ClueTransLogDO> list = clueTransLogService.getClueTransLogPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "规则执行记录.xls", "数据", ClueTransLogRespVO.class,
                        BeanUtils.toBean(list, ClueTransLogRespVO.class));
    }

}